package com.example.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {Exception.class})
    public Object exceptionHandler(HttpServletRequest request, Exception e) {
        log.error("未知异常，请求地址：{}, 错误信息：{}", request.getRequestURI(), e.getMessage());
        Map<String, Object> result = new HashMap<>();
        result.put("msg", e.getMessage());
        result.put("code", 500);
        return result;
    }
}
